package com.monitor.module.sediment.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.monitor.entity.pojo.sediment.Sediment;
import com.monitor.module.sediment.service.SedimentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.Instant;
import java.util.Date;

/**
 * 定时删除30天以前的数据库.
 *
 * @author Li Zhiyuan
 * @since 2020-11-23
 */
@Component
@Slf4j
public class TimingDatabase {
    @Autowired
    private SedimentService service;

    @Value("${sediment.days}")
    private Integer days;

    //每天两点删除30天以前的数据
    @Scheduled(cron = "0 0 2 * * *")
    public void deleteSdeiment() {

        //当前系统时间减去30天，删除30以前的数据
        Date date = Date.from(Instant.now().minus(Duration.ofDays(days)));
        log.info("当前系统时间减去{}天，删除 {} 以前的数据", days,date);
        service.remove(Wrappers.<Sediment>lambdaQuery().lt(Sediment::getCreateTime, date));
    }
}
